package com.fita.admin.web.controller;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.JsonUtils;
import com.fita.admin.handler.SmsHandler;
import com.fita.admin.service.DataService;
import com.fita.entity.order.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/sms")
public class SmsController {
	
	private static final Logger logger = LoggerFactory.getLogger(SmsController.class);
	
	@Autowired
	private SmsHandler smsHandler;
	
	@Autowired
	@Qualifier("dataService")
	private DataService<Order> orderService;
	
	/**
	 * 
	 * @param mobile
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value ="/cancel-order-message", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public HttpResult sendOrderCanCelMessage(@RequestParam("mobile") String mobile, @RequestParam("orderId") Long orderId) {
		
		HttpResult httpResult = new HttpResult();
		
		try {
			Order order = orderService.load(Order.class, orderId);
			
			boolean send = smsHandler.sendOrderCancelMessage(mobile, order.getAlamoCode(), "/cars/search");

			if(send) {
				
				httpResult.success().message("发送短信成功");

			} else {
				logger.error("#sendOrderCancelMessage() faild with response [{}]", JsonUtils.toJson(httpResult));
				httpResult.fail("501").message("发送短信失败");
			}
			
		} catch (Exception e) {
			logger.error("#sendOrderCancelMessage() occurs error", e); 
		}
		
		return httpResult;
	}
	
	

}
